﻿$(document).ready(function () {
    $("#ddl_certificate").on("change", function () {
        var cer = document.getElementById("ddl_certificate").value;
        if (cer == 0) {
            document.getElementById("test_area").style.display = "none";
            document.getElementById("edit_cer").style.display = "none";
            document.getElementById("question_area").style.display = 'none';
            document.getElementById("themcauhoi").style.display = 'none';
        }
        else {
            document.getElementById("question_area").style.display = 'none';
            document.getElementById("themcauhoi").style.display = 'none';
            document.getElementById("test_area").style.display = "block";
            document.getElementById("edit_cer").style.display = "inline-block";
            var xhttp;
            document.getElementById("test_list").innerHTML = '<div class="w3-row" style="text-align:center"><img src="../../../Content/img/loading.gif" style="width: 50%"/><div>';
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (xhttp.readyState == 4 && xhttp.status == 200) {
                    document.getElementById("test_list").innerHTML = xhttp.responseText;
                    $(".test").on("click", function () {
                        document.getElementById("question_area").style.display = "block";
                        document.getElementById("themcauhoi").style.display = "none";
                        document.getElementById("question_list").innerHTML = '<div class="w3-row" style="text-align:center"><img src="../../../Content/img/loading.gif" style="width: 50%"/><div>';
                        var id = this.getElementsByClassName("test_id")[0].innerHTML;
                        var xhttp;
                        xhttp = new XMLHttpRequest();
                        xhttp.onreadystatechange = function () {
                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                document.getElementById("question_list").innerHTML = xhttp.responseText;
                                $(".question").on("click", function () {
                                    var thutu = this.getElementsByClassName("thutu")[0].innerHTML;
                                    var id = this.getElementsByClassName("ques_id")[0].innerHTML;

                                    document.getElementById("themcauhoi").style.display = "block";
                                    var xhttp;
                                    xhttp = new XMLHttpRequest();
                                    xhttp.onreadystatechange = function () {
                                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                                            document.getElementById("themcauhoi").innerHTML = xhttp.responseText;
                                            $(".question_edit").on("click", function () {
                                                var name = $('#edit_ques_name').val();
                                                var id = document.getElementById('edit_ques_id').innerHTML;
                                                var pa1 = document.getElementsByClassName('question_detail')[0].value;
                                                var pa2 = document.getElementsByClassName('question_detail')[1].value;
                                                var pa3 = document.getElementsByClassName('question_detail')[2].value;
                                                var pa4 = document.getElementsByClassName('question_detail')[3].value;
                                                var thutu = document.getElementById("edit_ques_thutu").innerHTML;
                                                var file = document.getElementById("ques_file").value;
                                                var pa1_id = document.getElementById("pa1").value;
                                                var pa2_id = document.getElementById("pa2").value;
                                                var pa3_id = document.getElementById("pa3").value;
                                                var pa4_id = document.getElementById("pa4").value;
                                                var type = document.getElementById("test_detail_type").value;
                                                var confirm = "";
                                                if ($('#pa1').is(':checked'))
                                                    confirm = document.getElementById("pa1").value;
                                                if ($('#pa2').is(':checked'))
                                                    confirm = document.getElementById("pa2").value;
                                                if ($('#pa3').is(':checked'))
                                                    confirm = document.getElementById("pa3").value;
                                                if ($('#pa4').is(':checked'))
                                                    confirm = document.getElementById("pa4").value;
                                                if (name == "") {
                                                    alert("Nội dung không được để trống!");
                                                }
                                                else {
                                                    if (pa1 == "")
                                                        alert("Phương án 1 trống!");
                                                    else {
                                                        if (pa2 == "")
                                                            alert("Phương án 2 trống!");
                                                        else {
                                                            if (pa3 == "")
                                                                alert("Phương án 3 trống");
                                                            else {
                                                                if (pa4 == "")
                                                                    alert("Phương án 4 trống");
                                                                else {
                                                                    if (file != "") {
                                                                        var file_data = document.getElementById('ques_file').files[0];
                                                                        if (file_data == null) return; // chưa chọn file
                                                                        var form_data = new FormData();
                                                                        form_data.append("ques_file", file_data);
                                                                        $.ajax({
                                                                            url: '../ajax/asp/certificate/upload.aspx',
                                                                            data: form_data,
                                                                            processData: false,
                                                                            contentType: false,
                                                                            type: 'POST',
                                                                            success: function (data) {
                                                                                //$("#divTreeView").html(data);
                                                                            }
                                                                        });
                                                                    }
                                                                    var xhttp;
                                                                    xhttp = new XMLHttpRequest();
                                                                    xhttp.onreadystatechange = function () {
                                                                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                                            document.getElementById("themcauhoi").style.display = "none";
                                                                            document.getElementById(id).innerHTML = xhttp.responseText;
                                                                            $(".del_modal").click(function () {
                                                                                document.getElementById('xoacauhoi').style.display = 'block';
                                                                                document.getElementById('themcauhoi').style.display = 'none';
                                                                                $(".question").removeClass("selected");
                                                                                $(this).parent().addClass("selected");
                                                                            });
                                                                        }
                                                                    };
                                                                    xhttp.open("GET", "../ajax/asp/certificate/question_edited.aspx?id=" + id + "&thutu=" + thutu + "&name=" + name + "&pa1=" + pa1 + "&pa1_id=" + pa1_id + "&pa2=" + pa2 + "&pa2_id=" + pa2_id + "&pa3=" + pa3 + "&pa3_id=" + pa3_id + "&pa4=" + pa4 + "&pa4_id=" + pa4_id + "&file=" + file + "&confirm=" + confirm + "&type=" + type, true);
                                                                    xhttp.send();
                                                                    //alert(id+" "+ thutu+" " +name + " " + pa1 + " " +pa1_id+" " + pa2 + " " + pa3 + " " + pa4 + " " + file + " " + confirm);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }

                                            });
                                            $(".btn_edit_type").on("click", function () {
                                                var id = document.getElementById("test_detail_type").value;
                                                var xhttp;
                                                xhttp = new XMLHttpRequest();
                                                xhttp.onreadystatechange = function () {
                                                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                        document.getElementById("chinhsualoaich").innerHTML = xhttp.responseText;
                                                        document.getElementById("chinhsualoaich").style.display = 'block';
                                                        $(".btn_edited_type").on("click", function () {
                                                            var id = document.getElementById("test_type_id").innerHTML;
                                                            var type = document.getElementById("txt_test_type").value;
                                                            var xhttp;
                                                            xhttp = new XMLHttpRequest();
                                                            xhttp.onreadystatechange = function () {
                                                                if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                                    location.reload(true);
                                                                }
                                                            };
                                                            xhttp.open("GET", "../ajax/asp/test/edited_test_type.aspx?id=" + id + "&type=" + type, true);
                                                            xhttp.send();
                                                        });
                                                        $(".btn_del_type").on("click", function () {
                                                            var id = document.getElementById("test_type_id").innerHTML;
                                                            var xhttp;
                                                            xhttp = new XMLHttpRequest();
                                                            xhttp.onreadystatechange = function () {
                                                                if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                                    document.getElementById("xoaloaich").innerHTML = xhttp.responseText;
                                                                    document.getElementById("xoaloaich").style.display = 'block';
                                                                    document.getElementById("test_type_id").innerHTML = id;
                                                                    $(".btn_deleted_type").on("click", function () {
                                                                        var id = document.getElementById("test_type_id").innerHTML;
                                                                        var xhttp;
                                                                        xhttp = new XMLHttpRequest();
                                                                        xhttp.onreadystatechange = function () {
                                                                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                                                location.reload(true);
                                                                            }
                                                                        };
                                                                        xhttp.open("GET", "../ajax/asp/test/deleted_test_type.aspx?id=" + id, true);
                                                                        xhttp.send();
                                                                    });
                                                                }
                                                            };
                                                            xhttp.open("GET", "../ajax/asp/test/del_test_type.aspx?id=" + id, true);
                                                            xhttp.send();
                                                        });

                                                    }
                                                };
                                                xhttp.open("GET", "../ajax/asp/test/edit_test_type.aspx?id=" + id, true);
                                                xhttp.send();
                                            });
                                            $(".btn_add_type").on("click", function () {
                                                var type = document.getElementById("txt_add_type").value;
                                                if (type == "") {
                                                    alert("Loại câu hỏi không được để trống")
                                                }
                                                var xhttp;
                                                xhttp = new XMLHttpRequest();
                                                xhttp.onreadystatechange = function () {
                                                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                        location.reload(true);
                                                    }
                                                };
                                                xhttp.open("GET", "../ajax/asp/test/add_test_type.aspx?type=" + type, true);
                                                xhttp.send();
                                            });
                                        }
                                    };
                                    xhttp.open("GET", "../ajax/asp/certificate/question_edit.aspx?id=" + id + "&thutu=" + thutu, true);
                                    xhttp.send();
                                });
                                $(".del_modal").click(function () {
                                    document.getElementById('xoacauhoi').style.display = 'block';
                                    document.getElementById('themcauhoi').style.display = 'none';
                                    $(".question").removeClass("selected");
                                    $(this).parent().addClass("selected");
                                });
                            }
                        };

                        xhttp.open("GET", "../ajax/asp/certificate/get_test_detail.aspx?id=" + id, true);
                        xhttp.send();
                    });
                    $(".test_edit").click(function () {

                        var test_id = document.getElementById('get_id').innerHTML;
                        document.getElementById('suabai').style.display = "block";
                        document.getElementById("test_edit").innerHTML = '<div class="w3-row" style="text-align:center"><img src="../../../Content/img/loading.gif" style="width: 50%"/><div>';
                        var xhttp;
                        xhttp = new XMLHttpRequest();
                        xhttp.onreadystatechange = function () {
                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                document.getElementById("test_edit").innerHTML = xhttp.responseText;
                                $(".edit-on").click(function () {
                                    if ($(this).hasClass("off")) {
                                        $(this).removeClass("off");
                                        $(this).removeClass("fa-rotate-180");
                                        $(this).removeClass("w3-text-red");
                                        $(this).addClass("w3-text-white");
                                        document.getElementById("edit_active").innerHTML = '1';
                                    }
                                    else {
                                        $(this).addClass("w3-text-red");
                                        $(this).removeClass("w3-text-white");
                                        $(this).addClass("fa-rotate-180");
                                        $(this).addClass("off");
                                        document.getElementById("edit_active").innerHTML = '0';
                                    }

                                });
                                $(".edited").on("click", function () {
                                    var id = document.getElementById("get_id").innerHTML;
                                    var name = document.getElementById("edit_name").value;
                                    var time = document.getElementById("edit_time").value;
                                    var cer = document.getElementById("ddl_edit_certificate").value;
                                    var check = "";
                                    if ($('#thuong-edit').is(':checked'))
                                        check = "0";
                                    else
                                        check = "1";
                                    var start = document.getElementById("edit_start").value;
                                    var active = document.getElementById("edit_active").innerHTML;
                                    var level = "";
                                    for (var i = 0; i < document.getElementsByClassName('edit_user_level').length; i++) {
                                        if (document.getElementsByClassName('edit_user_level')[i].checked == true) {
                                            level += document.getElementsByClassName('edit_user_level')[i].value;
                                            level += ",";
                                        }
                                    }

                                    if (name == "") {
                                        alert("Tiêu đề không được để trống!");
                                    }
                                    else {
                                        if (time == "")
                                            alert("Thời gian làm bài không được để trống!");
                                        else {
                                            if ($('#thikt-edit').is(':checked') && start == "") {
                                                alert("Ngày mở bài không được để trống hoặc sai định dạng!");
                                            }
                                            else {
                                                var xhttp;
                                                xhttp = new XMLHttpRequest();
                                                xhttp.onreadystatechange = function () {
                                                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                        document.getElementById("suabai").style.display = "none";
                                                        document.getElementById("test" + id).innerHTML = xhttp.responseText;
                                                    }
                                                };
                                                xhttp.open("GET", "../ajax/asp/certificate/test_edited.aspx?id=" + id + "&name=" + name + "&cer=" + cer + "&time=" + time + "&check=" + check + "&start=" + start + "&level=" + level + "&active=" + active, true);
                                                xhttp.send();
                                                //alert(id+ " " +name + " " + time + " " + check + " " + active + " " + start + " " + level + " " + cer);
                                            }
                                        }
                                    }
                                });
                            }
                        };
                        xhttp.open("GET", "../ajax/asp/certificate/test_edit.aspx?id=" + test_id, true);
                        xhttp.send();
                    });
                    $("#edit_cer").on("click", function () {
                        var id = document.getElementById("ddl_certificate").value;

                        var xhttp;
                        xhttp = new XMLHttpRequest();
                        xhttp.onreadystatechange = function () {
                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                document.getElementById("chinhsuabang").innerHTML = xhttp.responseText;

                                document.getElementById("chinhsuabang").style.display = 'block';
                                $(".btn_edit_cer").on("click", function () {
                                    var id = document.getElementById("edit_cer_id").innerHTML;
                                    var cer = document.getElementById("txt_edit_cer").value;
                                    if (cer == "") {
                                        alert("Tên quyền không được để trống!");
                                    }
                                    else {
                                        var xhttp;
                                        xhttp = new XMLHttpRequest();
                                        xhttp.onreadystatechange = function () {
                                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                location.reload(true);
                                            }
                                        };
                                        xhttp.open("GET", "../ajax/asp/certificate/edited_cer.aspx?cer=" + cer + "&id=" + id, true);
                                        xhttp.send();
                                    }
                                });
                                $(".btn_del_cer").on("click", function () {
                                    var id = document.getElementById("edit_cer_id").innerHTML;
                                    var xhttp;
                                    xhttp = new XMLHttpRequest();
                                    xhttp.onreadystatechange = function () {
                                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                                            document.getElementById("xoabang").innerHTML = xhttp.responseText;
                                            document.getElementById("xoabang").style.display = 'block';
                                            $(".btn_deleted_cer").on("click", function () {
                                                var id = document.getElementById("edit_cer_id").innerHTML;
                                                var xhttp;
                                                xhttp = new XMLHttpRequest();
                                                xhttp.onreadystatechange = function () {
                                                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                        location.reload(true);

                                                    }
                                                };
                                                xhttp.open("GET", "../ajax/asp/certificate/deleted_cer.aspx?id=" + id, true);
                                                xhttp.send();
                                            });
                                        }
                                    };
                                    xhttp.open("GET", "../ajax/asp/certificate/del_cer.aspx?id=" + id, true);
                                    xhttp.send();
                                });
                            }
                        };
                        xhttp.open("GET", "../ajax/asp/certificate/edit_cer.aspx?id=" + id, true);
                        xhttp.send();
                    });

                }
            };
            xhttp.open("GET", "../ajax/asp/certificate/get_test.aspx?cer=" + cer, true);
            xhttp.send();
        }
    });

    $(".btn_add_test").click(function () {
        var title = document.getElementById("new_test_name").value;
        var time = document.getElementById("new_test_time").value;
        var check = "";
        if ($('#thuong').is(':checked'))
            check = "0";
        else
            check = "1";
        var start = document.getElementById("new_test_start").value;
        var level = "";
        for (var i = 0; i < document.getElementsByClassName('user_level').length; i++) {
            if (document.getElementsByClassName('user_level')[i].checked == true) {
                level += document.getElementsByClassName('user_level')[i].value;
                level += ",";
            }
        }
        var active = document.getElementById("active").innerHTML;
        var cer = document.getElementById("ddl_certificate").value;
        if (title == "")
            alert("Tiêu đề không được để trống!");
        else {
            if (time == "")
                alert("Thời gian làm bài không được để trống!");
            else {
                if ($('#thikt').is(':checked') && start == "") {
                    alert("Ngày mở bài không được để trống hoặc sai định dạng");
                }
                else {
                    //alert(title + cer + time + check + start + level + active);
                    var xhttp;
                    xhttp = new XMLHttpRequest();
                    xhttp.onreadystatechange = function () {
                        if (xhttp.readyState == 4 && xhttp.status == 200) {
                            document.getElementById("thembai").style.display = "none";
                            document.getElementById("test_list").innerHTML += xhttp.responseText;
                            $(".test").on("click", function () {
                                document.getElementById("themcauhoi").style.display = "none";
                                document.getElementById("question_area").style.display = "block";
                                document.getElementById("question_list").innerHTML = '<div class="w3-row" style="text-align:center"><img src="../../../Content/img/loading.gif" style="width: 50%"/><div>';
                                var id = this.getElementsByClassName("test_id")[0].innerHTML;
                                var xhttp;
                                xhttp = new XMLHttpRequest();
                                xhttp.onreadystatechange = function () {
                                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                                        document.getElementById("question_list").innerHTML = xhttp.responseText;
                                        $(".question").on("click", function () {
                                            var thutu = this.getElementsByClassName("thutu")[0].innerHTML;
                                            var id = this.getElementsByClassName("ques_id")[0].innerHTML;
                                            document.getElementById("themcauhoi").style.display = "block";
                                            var xhttp;
                                            xhttp = new XMLHttpRequest();
                                            xhttp.onreadystatechange = function () {
                                                if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                    document.getElementById("themcauhoi").innerHTML = xhttp.responseText;
                                                    $(".question_edit").on("click", function () {
                                                        var name = $('#edit_ques_name').val();
                                                        var id = document.getElementById('edit_ques_id').innerHTML;
                                                        var pa1 = document.getElementsByClassName('question_detail')[0].value;
                                                        var pa2 = document.getElementsByClassName('question_detail')[1].value;
                                                        var pa3 = document.getElementsByClassName('question_detail')[2].value;
                                                        var pa4 = document.getElementsByClassName('question_detail')[3].value;
                                                        var thutu = document.getElementById("edit_ques_thutu").innerHTML;
                                                        var file = document.getElementById("ques_file").value;

                                                        var pa1_id = document.getElementById("pa1").value;
                                                        var pa2_id = document.getElementById("pa2").value;
                                                        var pa3_id = document.getElementById("pa3").value;
                                                        var pa4_id = document.getElementById("pa4").value;
                                                        var type = document.getElementById("test_detail_type").value;

                                                        var confirm = "";
                                                        if ($('#pa1').is(':checked'))
                                                            confirm = document.getElementById("pa1").value;
                                                        if ($('#pa2').is(':checked'))
                                                            confirm = document.getElementById("pa2").value;
                                                        if ($('#pa3').is(':checked'))
                                                            confirm = document.getElementById("pa3").value;
                                                        if ($('#pa4').is(':checked'))
                                                            confirm = document.getElementById("pa4").value;
                                                        if (name == "") {
                                                            alert("Nội dung không được để trống!");
                                                        }
                                                        else {
                                                            if (pa1 == "")
                                                                alert("Phương án 1 trống!");
                                                            else {
                                                                if (pa2 == "")
                                                                    alert("Phương án 2 trống!");
                                                                else {
                                                                    if (pa3 == "")
                                                                        alert("Phương án 3 trống");
                                                                    else {
                                                                        if (pa4 == "")
                                                                            alert("Phương án 4 trống");
                                                                        else {
                                                                            if (file != "") {
                                                                                var file_data = document.getElementById('ques_file').files[0];
                                                                                if (file_data == null) return; // chưa chọn file
                                                                                var form_data = new FormData();
                                                                                form_data.append("ques_file", file_data);
                                                                                $.ajax({
                                                                                    url: '../ajax/asp/certificate/upload.aspx',
                                                                                    data: form_data,
                                                                                    processData: false,
                                                                                    contentType: false,
                                                                                    type: 'POST',
                                                                                    success: function (data) {
                                                                                        //$("#divTreeView").html(data);
                                                                                    }
                                                                                });
                                                                            }
                                                                            var xhttp;
                                                                            xhttp = new XMLHttpRequest();
                                                                            xhttp.onreadystatechange = function () {
                                                                                if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                                                    document.getElementById("themcauhoi").style.display = "none";
                                                                                    document.getElementById(id).innerHTML = xhttp.responseText;
                                                                                    $(".del_modal").click(function () {
                                                                                        document.getElementById('xoacauhoi').style.display = 'block';
                                                                                        document.getElementById('themcauhoi').style.display = 'none';
                                                                                        $(".question").removeClass("selected");
                                                                                        $(this).parent().addClass("selected");
                                                                                    });
                                                                                }
                                                                            };
                                                                            xhttp.open("GET", "../ajax/asp/certificate/question_edited.aspx?id=" + id + "&thutu=" + thutu + "&name=" + name + "&pa1=" + pa1 + "&pa1_id=" + pa1_id + "&pa2=" + pa2 + "&pa2_id=" + pa2_id + "&pa3=" + pa3 + "&pa3_id=" + pa3_id + "&pa4=" + pa4 + "&pa4_id=" + pa4_id + "&file=" + file + "&confirm=" + confirm + "&type=" + type, true);
                                                                            xhttp.send();
                                                                            //alert(id+" "+ thutu+" " +name + " " + pa1 + " " +pa1_id+" " + pa2 + " " + pa3 + " " + pa4 + " " + file + " " + confirm);
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    });
                                                }
                                            };
                                            xhttp.open("GET", "../ajax/asp/certificate/question_edit.aspx?id=" + id + "&thutu=" + thutu, true);
                                            xhttp.send();
                                        });
                                        $(".del_modal").click(function () {
                                            document.getElementById('xoacauhoi').style.display = 'block';
                                            document.getElementById('themcauhoi').style.display = 'none';
                                            $(".question").removeClass("selected");
                                            $(this).parent().addClass("selected");
                                        });
                                    }
                                };

                                xhttp.open("GET", "../ajax/asp/certificate/get_test_detail.aspx?id=" + id, true);
                                xhttp.send();
                            });
                        }
                    };
                    xhttp.open("GET", "../ajax/asp/certificate/add_test.aspx?title=" + title + "&cer=" + cer + "&time=" + time + "&check=" + check + "&start=" + start + "&level=" + level + "&active=" + active, true);
                    xhttp.send();
                }
            }
        }

    });

    $(".btn_add_cer").on("click", function () {
        var cer = document.getElementById("txt_add_cer").value;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                location.reload(true);
            }
        };
        xhttp.open("GET", "../ajax/asp/certificate/add_cer.aspx?cer=" + cer, true);
        xhttp.send();
    });


});